<template>
  <drop-wrap :data="data" :index="index" :element="element">
    <div class="choose_person_wrap">
      <div class="left-tree-wrap">
        <el-input
          class="search-tree"
          suffix-icon="el-icon-search"
          size="small"
          placeholder="输入关键字进行过滤"
          v-model="filterText"
        >
        </el-input>
        <el-tree
          ref="tree"
          :filter-node-method="filterNode"
          :data="treeData"
          :props="defaultProps"
          @node-click="handleNodeClick"
        ></el-tree>
      </div>
      <div class="right-table-wrap">
        <div class="can-select" style="width: 46%">
          <el-table
            ref="can-select-table"
            border
            :data="leftTableData"
            tooltip-effect="dark"
            style="width: 100%"
            height="380"
            @selection-change="leftHandleSelectionChange"
            @cell-dblclick="leftCellDblclick"
          >
            <el-table-column type="selection" fixed width="45">
            </el-table-column>
            <el-table-column label="日期" width="120">
              <template slot-scope="scope">{{ scope.row.date }}</template>
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="120">
            </el-table-column>
            <el-table-column prop="address" label="地址" show-overflow-tooltip>
            </el-table-column>
          </el-table>
          <div style="padding-top: 20px">
            <el-pagination background layout="prev, pager, next" :total="1000">
            </el-pagination>
          </div>
        </div>

        <div class="middle-select" style="width: 10%">
          <el-button
            @click="leftToRight"
            :type="leftMultipleSelection.length > 0 ? 'primary' : null"
            circle
            icon="el-icon-d-arrow-right"
          ></el-button>
          <el-button
            @click="rightToLeft"
            :type="rightMultipleSelection.length > 0 ? 'primary' : null"
            circle
            icon="el-icon-d-arrow-left"
          ></el-button>
        </div>

        <div class="has-select" style="width: 46%">
          <el-table
            ref="has-select-table"
            border
            :data="rightTableData"
            tooltip-effect="dark"
            style="width: 100%"
            height="380"
            @selection-change="rightHandleSelectionChange"
             @cell-dblclick="rightCellDblclick"
          >
            <el-table-column type="selection" fixed width="45">
            </el-table-column>
            <el-table-column label="日期" width="120">
              <template slot-scope="scope">{{ scope.row.date }}</template>
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="120">
            </el-table-column>
            <el-table-column prop="address" label="地址" show-overflow-tooltip>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </drop-wrap>
</template>

<script>
export default {
  props: ["element", "data", "index"],
  data() {
    return {
      filterText: "",
      treeData: [
        {
          label: "一级 1",
          children: [
            {
              label: "二级 1-1",
              children: [
                {
                  label: "三级 1-1-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 2",
          children: [
            {
              label: "二级 2-1",
              children: [
                {
                  label: "三级 2-1-1",
                },
              ],
            },
            {
              label: "二级 2-2",
              children: [
                {
                  label: "三级 2-2-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 3",
          children: [
            {
              label: "二级 3-1",
              children: [
                {
                  label: "三级 3-1-1",
                },
              ],
            },
            {
              label: "二级 3-2",
              children: [
                {
                  label: "三级 3-2-1",
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
      /** */
      leftTableData: [
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-08",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-06",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-07",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
      ],
      rightTableData: [],
      leftMultipleSelection: [],
      rightMultipleSelection: [],
    };
  },
  methods: {
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleNodeClick(data) {
      console.log(data);
    },
    /** */
    leftHandleSelectionChange(val) {
      this.leftMultipleSelection = val;
    },
    rightHandleSelectionChange(val) {
      this.rightMultipleSelection = val;
    },
    leftToRight() {
      if (this.leftMultipleSelection.length > 0) {
        let arr = [...this.rightTableData, ...this.leftMultipleSelection];
        this.rightTableData = [...Array.from(new Set(arr))];
        this.$refs["can-select-table"].clearSelection();
      }
    },
    //两个数组的差集
    rightToLeft() {
      let minus = this.rightTableData.filter(
        (x) => !new Set(this.rightMultipleSelection).has(x)
      );
      this.rightTableData = minus;
    },
    leftCellDblclick(row){
        let arr = [...this.rightTableData];
        arr.push(row)
        this.rightTableData = [...Array.from(new Set(arr))];
    },
    rightCellDblclick(row){
      let minus = this.rightTableData.filter(
        (x) => !new Set([row]).has(x)
      );
      this.rightTableData = minus;
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },
  computed: {
    store() {
      return  this.element.getRoot();
    },
  },
};
</script>

<style lang="scss" scoped>
* {
  box-sizing: border-box;
}
.choose_person_wrap {
  display: flex;
  padding: 15px;
  min-height: 450px;
  .left-tree-wrap {
    flex-shrink: 0;
    width: 200px;
    overflow: auto;
    border-radius: 4px;
    box-shadow: 0px 0px 6px rgba(5, 1, 1, 0.1);
    padding: 8px;
    .search-tree {
      margin-bottom: 10px;
    }
    margin-right: 25px;
  }
  .middle-select {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin: 0 10px;
    transform: translateY(-20px);
    & > .el-button {
      width: 40px;
      height: 40px;
    }
    & > .el-button:nth-child(1) {
      margin-bottom: 15px;
    }
  }

  .right-table-wrap {
    width: calc(100% - 300px);
    display: flex;
    justify-content: flex-start;
  }

/* 整个滚动条背景 */
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-thumb {
  width: 15px;
  /* 上下两头给个圆角 */
  background-color: #dbdbdb;
  border-radius: 10px;

}
/* 滚动条轨道 */
::-webkit-scrollbar-track {
  border-radius: 10px;
  background-color: rgba(228, 228, 228, 0.568);
}


}

.el-button + .el-button {
  margin-left: unset;
}



</style>